package cn.mw.cmdb.entity;

import cn.mw.components.mongodb.anno.CMDBProperty;
import cn.mw.components.mongodb.anno.DataAuthAnno;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Data
public abstract class DataAuth {
    //是否内置属性
    @CMDBProperty
    private Boolean internal = false;

    //所属用户组
    @DataAuthAnno(InputDataAuth.GroupKey)
    @CMDBProperty(searchType = PropertyTypeGroup.ID)
    private List<Long> groups;

    //所属用户
    @DataAuthAnno(InputDataAuth.UserKey)
    @NotNull(message = "{principal.notempty}")
    @CMDBProperty(searchType = PropertyTypeList.ID)
    private List<Long> users;

    //所属机构id
    //便于数据权限查询时,只查询自己机构的数据
    @CMDBProperty(searchType = PropertyTypeOrg.ID)
    private List<Long> orgIds;

    @DataAuthAnno(value = InputDataAuth.OrgKey)
    @CMDBProperty(searchType = PropertyTypeOrg.ID)
    @NotNull(message = "{org.notempty}")
    private List<List<Long>> orgs;

    private static Map<String ,String> fieldNameMap = new HashMap<>();

    static {
        fieldNameMap.put("users" ,"userNames");
        fieldNameMap.put("orgs" ,"orgNames");
        fieldNameMap.put("orgIds" ,"orgNames");
        fieldNameMap.put("groups" ,"groupNames");
    }

    /*
     *数据权限类型,custom则需要根据机构,用户组,用户过滤
     * buildIn是内置类型,数据对所有机构,用户组,用户可见,此类型由开发人员从数据库增加
     */
    private String dataAuthType = DataAuthType.custom.name();

    public void setOrgs( List<List<Long>> orgs) {
        this.orgIds = new ArrayList<>();
        if(null != orgs){
            for(List value : orgs){
                Long valueLong = Long.valueOf(value.get(value.size() - 1).toString());
                orgIds.add(valueLong);
            }
        }
        this.orgs = orgs;
    }

    public static String getNamesFieldName(String field){
        return fieldNameMap.get(field);
    }
}
